package com.yike.user.lkl.api;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lkl.laop.sdk.LKLSDK;
import com.lkl.laop.sdk.exception.SDKException;
import com.lkl.laop.sdk.request.*;
import com.lkl.laop.sdk.request.model.V3LabsTradeLocationInfo;
import com.yike.common.Response.*;
import com.yike.common.entity.SysInfo;
import com.yike.common.request.ApplyForSepAccReq;
import com.yike.common.tool.IpUtil;
import com.yike.user.entity.ShopDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;


/**
 * 分账相关api接口
 */
@Slf4j
@Component
public class V2SeparateAccounts {

    @Value("${lkl.org_code}")
    private String  orgCode;
    @Value("${lkl.not_url}")
    private String  notUrl;

    /**
     * 文件上传
     * @param OrderNo
     * @param attType
     * @param attExtName
     * @param attContext base64编码
     * @return
     * @throws SDKException
     */
    public LklFileResponse fileUpload(String OrderNo,String attType, String attExtName, String  attContext) throws SDKException {
        V2MmsOpenApiUploadFileRequest reqFile = new V2MmsOpenApiUploadFileRequest();
        reqFile.setVersion("1.0");
        reqFile.setOrgCode(this.orgCode);
        reqFile.setOrderNo(OrderNo);
        reqFile.setAttType(attType);
        reqFile.setAttContext(attContext);
        reqFile.setAttExtName(attExtName);
        log.info("发起文件上传，请求参数：{}",reqFile.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(reqFile);
        log.info("发起文件上传-返回内容，response:{}",response);

        LklV2Response lklResponse = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklResponse.getRetCode().equals("000000")){
            throw new RuntimeException(lklResponse.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklResponse.getRespData()), LklFileResponse.class);
    }




    /**
     * 分账申请
     * @param OrderNo
     * @param req
     * @return
     * @throws SDKException
     */
    public LklApplyLedgerMerResponse applyLedgerMer(String OrderNo, ApplyForSepAccReq req) throws SDKException {
        V2MmsOpenApiLedgerApplyLedgerMerRequest request = new V2MmsOpenApiLedgerApplyLedgerMerRequest();
        request.setVersion("1.0");
        request.setOrgCode(this.orgCode);
        request.setOrderNo(OrderNo);
//        request.setMerInnerNo(req.getCustomerNo());
        request.setMerCupNo(req.getExternalCustomerNo());
        request.setContactMobile(req.getContactMobile());

        BigDecimal bigDecimal = new BigDecimal("100.00");
        request.setSplitLowestRatio(bigDecimal.subtract(req.getCommissionRate()));

        request.setSplitEntrustFileName( req.getSplitEntrustFileName());
        request.setSplitEntrustFilePath(req.getSplitEntrustFilePath());
        request.setSplitRange("ALL");

        request.setRetUrl(this.notUrl+"/api-user/lkl/notice/applyLedgerMer");  //回调地址

        log.info("发起商户分账业务开通申请，请求参数：{}",request.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起商户分账业务开通申请-返回内容，response:{}",response);

        LklV2Response lklV2Response = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklV2Response.getRetCode().equals("000000")){
            throw new RuntimeException(lklV2Response.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklV2Response.getRespData()), LklApplyLedgerMerResponse.class);
    }


    /**
     * 商户分账信息变更申请
     * @param OrderNo
     * @param req
     * @return
     * @throws SDKException
     */
    public LklFileResponse modifyLedgerMer(String OrderNo, ApplyForSepAccReq req) throws SDKException {
        V2MmsOpenApiLedgerModifyLedgerMerRequest request = new V2MmsOpenApiLedgerModifyLedgerMerRequest();
        request.setVersion("1.0");
        request.setOrgCode(this.orgCode);
        request.setOrderNo(OrderNo);
        request.setMerInnerNo(req.getCustomerNo());
        request.setContactMobile(req.getContactMobile());

        BigDecimal bigDecimal = new BigDecimal("100.00");
        request.setSplitLowestRatio(String.valueOf(bigDecimal.subtract(req.getCommissionRate())));

        request.setSplitEntrustFileName(req.getSplitEntrustFileName());
        request.setSplitRange("ALL");

        request.setRetUrl(this.notUrl+"/api-user/lkl/notice/applyLedgerMerUpdate");  //回调地址

        log.info("发起商户分账信息变更申请，请求参数：{}",request.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起商户分账信息变更申请-返回内容，response:{}",response);

        LklV2Response lklV2Response = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklV2Response.getRetCode().equals("000000")){
            throw new RuntimeException(lklV2Response.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklV2Response.getRespData()), LklFileResponse.class);
    }


    /**
     * 分账接收方创建申请
     * @param OrderNo
     * @param sysInfo
     * @return
     * @throws SDKException
     */
    public LklApplyLedgerReceiverResponse applyLedgerReceiver(String OrderNo, SysInfo sysInfo) throws SDKException {
        V2MmsOpenApiLedgerApplyLedgerReceiverRequest request = new V2MmsOpenApiLedgerApplyLedgerReceiverRequest();
        BeanUtils.copyProperties(sysInfo, request);
        request.setVersion("1.0");
        request.setOrgCode(this.orgCode);
        request.setOrderNo(OrderNo);
        request.setReceiverName(sysInfo.getName());
        request.setContactMobile(sysInfo.getMobile());

        log.info("发起分账接收方创建申请，请求参数：{}",request.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起分账接收方创建申请-返回内容，response:{}",response);

        LklV2Response lklV2Response = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklV2Response.getRetCode().equals("000000")){
            throw new RuntimeException(lklV2Response.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklV2Response.getRespData()), LklApplyLedgerReceiverResponse.class);
    }



    /**
     * 分账接收方信息变更
     * @param OrderNo
     * @param sysInfo
     * @return
     * @throws SDKException
     */
    public LklFileResponse modifyLedgerReceiver(String OrderNo, SysInfo sysInfo) throws SDKException {
        V2MmsOpenApiLedgerApplyLedgerReceiverRequest request = new V2MmsOpenApiLedgerApplyLedgerReceiverRequest();
        BeanUtils.copyProperties(sysInfo, request);
        request.setVersion("1.0");
        request.setOrgCode(this.orgCode);
        request.setOrderNo(OrderNo);
        request.setReceiverName(sysInfo.getName());

        log.info("发起分账接收方信息变更申请，请求参数：{}",request.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起分账接收方信息变更变更申请-返回内容，response:{}",response);

        LklV2Response lklV2Response = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklV2Response.getRetCode().equals("000000")){
            throw new RuntimeException(lklV2Response.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklV2Response.getRespData()), LklFileResponse.class);
    }



    /**
     * 分账关系绑定申请
     * @param OrderNo
     * @param sysInfo
     * @return
     * @throws SDKException
     */
    public LklApplyLedgerMerResponse applyBind(String OrderNo,ShopDetails shopDetails, SysInfo sysInfo,String entrustFileName,String entrustFilePath) throws SDKException {
        V2MmsOpenApiLedgerApplyBindRequest request = new V2MmsOpenApiLedgerApplyBindRequest();
        BeanUtils.copyProperties(sysInfo, request);
        request.setVersion("1.0");
        request.setOrgCode(this.orgCode);
        request.setOrderNo(OrderNo);
        request.setMerInnerNo(shopDetails.getCustomerNo());
        request.setMerCupNo(shopDetails.getExternalCustomerNo());
        request.setReceiverNo(sysInfo.getReceiverNo());
        request.setEntrustFileName(entrustFileName);
        request.setEntrustFilePath(entrustFilePath);
        request.setRetUrl(this.notUrl+"/api-user/lkl/notice/applyBindNot");
        log.info("发起分账关系绑定申请，请求参数：{}",request.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起分账关系绑定申请-返回内容，response:{}",response);

        LklV2Response lklV2Response = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklV2Response.getRetCode().equals("000000")){
            throw new RuntimeException(lklV2Response.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklV2Response.getRespData()), LklApplyLedgerMerResponse.class);
    }





    /**
     * 商户分账信息查询
     * @param OrderNo
     * @param merInnerNo  //内部商户号
     * @return
     * @throws SDKException
     */
    public Map<String ,Object> queryLedgerMer(String OrderNo,String merInnerNo) throws SDKException {
        V2MmsOpenApiLedgerQueryLedgerMerRequest request = new V2MmsOpenApiLedgerQueryLedgerMerRequest();
        request.setVersion("1.0");
        request.setOrgCode(this.orgCode);
        request.setOrderNo(OrderNo);
        request.setMerInnerNo(merInnerNo);


        log.info("发起商户分账信息查询，请求参数：{}",request.toBody());

        //3. 发送请求
        String response = LKLSDK.httpPost(request);
        log.info("发起商户分账信息查询-返回内容，response:{}",response);

        LklV2Response lklV2Response = JSONObject.parseObject(response,LklV2Response.class);
        if(!lklV2Response.getRetCode().equals("000000")){
            throw new RuntimeException(lklV2Response.getRetMsg());
        }

        return JSON.parseObject(JSON.toJSONString(lklV2Response.getRespData()), Map.class);
    }



}
